package src.list;

import java.security.PublicKey;
import java.util.Iterator;
import java.util.List;
import java.util.Stack;

/**
 * @author starsea
 * @date 2024-08-14 15:19
 */

public class SingleLinkedList {
    static class ListNode {
        private int val;
        private ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }
    public boolean chkPalindrome(ListNode a) {
        ListNode slow=a;
        ListNode fast=a;
        ListNode cur=a;
        //找到中间的节点
        while(fast!=null && fast.next!=null)
        {
            slow=slow.next;
            fast=fast.next.next;
        }
        cur=slow.next;
        while(cur!=null)
        {
            ListNode curNext=cur.next;
            cur.next=slow;
            slow=cur;
            cur=curNext;
        }
        while(a!=slow)
        {
            if(a.val!=slow.val)
            {
                return false;
            }
            if(a.next==slow.next)
            {
                return true;
            }
            a=a.next;
            slow=slow.next;
        }
        return true;
    }

    //创建头指针
    public ListNode head;
    int usedSize;

    //头插法
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        if(head==null)
        {
            head=node;
            return;
        }
        node.next = head;
        head = node;
        usedSize++;

    }

    //尾插法
    public void addLast(int data) {

        ListNode node = new ListNode(data);
        if(head==null)
        {
            head=node;
            return;
        }
        ListNode cur = head;
        while (cur.next != null) {
            cur = cur.next;
        }
        cur.next = node;
        usedSize++;
    }

    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index, int data) {
        ListNode node = new ListNode(data);
        ListNode cur = head;
        if(index<0 && index>usedSize)
        {
            System.out.println("输入的位置有误，无法插入");
            return;
        }
        while (index>0)
        {
            cur=cur.next;
            index--;
        }
        node.next=cur.next;
        cur.next=node;
        usedSize++;
    }

    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key) {
        ListNode cur = head;
        while (cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //删除第一次出现关键字为key的节点
    public void remove(int key) {
        ListNode cur = head;
        ListNode pre = head;
        while (cur != null) {
            if (cur.val == key) {
               pre.next=cur.next;
               return;
            }
            pre=cur;
            cur=cur.next;
        }
        usedSize--;
    }

    //删除所有值为key的节点
    public void removeAllKey(int key) {
        ListNode cur = head;
        ListNode pre = head;
        while (cur != null) {
            if (cur.val == key) {
                pre.next=cur.next;
                cur=pre.next;
                usedSize--;
                continue;
            }
            pre=cur;
            cur=cur.next;
        }

    }

    //得到单链表的长度
    public int size() {
        return usedSize;
    }

    public void clear() {
        head = null;
        usedSize=0;
    }

    public void display() {
        if(usedSize==0)
        {
            System.out.println("链表为空");
        }
        ListNode cur = head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }
}
